<?php
include "../../../include/cp_header.php";
require_once "../../../mainfile.php";
require_once'../xoops_version.php';
include_once XOOPS_ROOT_PATH."/modules/Top_Menu/admin/functions.php";
xoops_cp_header();

if(!isset($_GET['currentID'])){
	$_GET['currentID'] = 0;
}
//adminmenu($_GET['currentID']);//creates tabs in admin section
groovyMenu($_GET['currentID']);

/* 
$op = '';
foreach ($_POST as $k => $v) {
	${$k} = $v;
}

if (isset($_GET['op'])) {
	$op = $_GET['op'];
	if (isset($_GET['id'])) {
		$id = intval($_GET['id']);
	}
}
switch($op) {
case "new":
	im_admin_new();
	break;
case "edit":
	im_admin_edit($id);
	break;
case "update":
	im_admin_update($id, $title, $link, $hide, $groups, $target, $modules);
	break;
case "del":
	im_admin_del($id, $del);
	break;
case "move":
	im_admin_move($id, $_GET['weight']);
	im_admin_list();
	break;
case "styles":
	echo "Styles Yo";
	xoops_cp_footer();
	break;	
default:
	im_admin_list();
	break;
}

function im_admin_update($id, $title, $link, $hide, $groups, $target, $modules) {
	$xoopsDB =& Database::getInstance();
	$myts =& MyTextSanitizer::getInstance();
	$title = $myts->makeTboxData4Save($title);
	$link = $myts->makeTboxData4Save($link);
	$groups = (is_array($groups)) ? implode(" ", $groups) : '';
	if ( empty($id) ) {
		$newid = $xoopsDB->genId($xoopsDB->prefix("tmenu")."_id_seq");
		$success = $xoopsDB->query("INSERT INTO ".$xoopsDB->prefix("tmenu")." (id,title,hide,link,weight,groups,target,module) VALUES ($newid,'$title','$hide','$link','255','$groups','$target','$modules')");
		im_admin_clean();
	} else 	$success = $xoopsDB->query("UPDATE ".$xoopsDB->prefix("tmenu")." SET title='$title', hide='$hide', link='$link', groups='$groups', target='$target', module='$modules' WHERE id='$id'");
	if ( !$success ) redirect_header("index.php",2,_IM_UPDATED);
	else redirect_header("index.php",2,_IM_UPDATED);
	exit();
}

function im_admin_edit ($id) {
	//xoops_cp_header();
	$xoopsDB =& Database::getInstance();
	$result = $xoopsDB->query("SELECT title, hide, link, groups, target, module FROM ".$xoopsDB->prefix("tmenu")." WHERE id=$id");
	$resultMods = $xoopsDB->query("SELECT dirname, name FROM ".$xoopsDB->prefix("modules") ."");
	
	$modArray = array();
	$x=0;
	while ( $myrow = $xoopsDB->fetchArray($resultMods) ) {
		$modArray[$x]['name'] = $myrow['name'];
		$modArray[$x]['module'] = $myrow['dirname'];
		++$x;
    }//end while
	
	
	//list($dirname) = $xoopsDB->fetchrow($resultMods);
	list($title, $hide, $link, $groups, $target, $module) = $xoopsDB->fetchrow($result);
	$groups = explode(" ", $groups);
	include XOOPS_ROOT_PATH."/class/xoopsformloader.php";
	$form = new XoopsThemeForm(_IM_EDITIMENU, "editform", "index.php");
	include '../imenuform.inc.php';
	$form->display();
	xoops_cp_footer();
}

function im_admin_del($id, $del=0) {
	$xoopsDB =& Database::getInstance();
	if ( $del == 1 ) {
		if ( $xoopsDB->query("DELETE FROM ".$xoopsDB->prefix("tmenu")." WHERE id=$id") ) {
			im_admin_clean();
			redirect_header("index.php", 2, _IM_UPDATED);
		} else {
			redirect_header("index.php", 2, _IM_NOTUPDATED);
		}
		exit();
	} else {
		xoops_confirm(array('op' => 'del', 'id' => $id, 'del' => 1), 'index.php', _IM_SUREDELETE);
		xoops_cp_footer();
	}
}

function im_admin_move ($id, $weight) {
	$xoopsDB =& Database::getInstance();
	$xoopsDB->queryF("UPDATE ".$xoopsDB->prefix("tmenu")." SET weight=weight+1 WHERE weight>=$weight AND id<>$id");
	$xoopsDB->queryF("UPDATE ".$xoopsDB->prefix("tmenu")." SET weight=$weight WHERE id=$id");
	im_admin_clean(); 
}

function im_admin_new() {
    $xoopsDB =& Database::getInstance();
    $resultMods = $xoopsDB->query("SELECT dirname, name FROM ".$xoopsDB->prefix("modules") ."");
	$modArray = array();
	$x=0;
	while ( $myrow = $xoopsDB->fetchArray($resultMods) ) {
		$modArray[$x]['name'] = $myrow['name'];
		$modArray[$x]['module'] = $myrow['dirname'];
		++$x; 
    }//end while
	

	//xoops_cp_header();
	$id = 0;
	$title = '';
	$link = '';
	$hide = '';
	$weight = 255;
	$target = "_self";
	$member_handler =& xoops_gethandler('member');
	$xoopsgroups =& $member_handler->getGroups();
	$count = count($xoopsgroups);
	$groups = array();
	for ($i = 0; $i < $count; $i++)  $groups[] = $xoopsgroups[$i]->getVar('groupid');
	include XOOPS_ROOT_PATH."/class/xoopsformloader.php";
	$form = new XoopsThemeForm(_IM_NEWIMENU, "newform", "index.php");
	include '../imenuform.inc.php';
	$form->display();
	xoops_cp_footer();
}

function im_admin_list() {
	//xoops_cp_header();
	$xoopsDB =& Database::getInstance();
	echo "<form action='index.php?op=new' method='post' name='form1'>
	<table width='100%' border='0' cellspacing='1' cellpadding='0' class='outer'><tr>
	<th align='center'>"._IM_TITLE."</th>
	<th align='center'>"._IM_HIDE."</th>
	<th align='center'>"._IM_LINK."</th>
	<th align='center'>Associated Module</th>
	<th align='center'>"._IM_OPERATION."</th></tr>";
	$result=$xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("tmenu")." ORDER BY weight ASC");
	$class = 'even';
	while ($row=$xoopsDB->fetcharray($result)) {
		$status = ( $row['hide'] == 0 ) ? _NO : _YES;
		if ($row['weight'] != 0) $moveup = "<a href='index.php?op=move&id=".$row['id']."&weight=".($row['weight']-1)."'><img src='../images/up.gif' /></a>"; else $moveup = " <img src='../images/noup.gif' /> ";
		if ($row['weight'] != ($xoopsDB->getRowsNum($result) - 1)) $movedown = "<a href='index.php?op=move&id=".$row['id']."&weight=".($row['weight']+2)."'> <img src='../images/down.gif' /> </a>"; else $movedown = " <img src='../images/nodown.gif' /> ";
		echo "<tr>
			<td class='$class'>".$row['title']."</td>
			<td class='$class' align='center'>$status</td>
			<td class='$class'>".$row['link']."</td>
			<td class='$class'>".$row['module']."</td>
			<td class='$class' align='center'><small><a href='index.php?op=del&id=".$row['id']."'>"._DELETE." | </a>
			<a href='index.php?op=edit&id=".$row['id']."'> "._EDIT."  &nbsp; &nbsp; </a>".$moveup. " " .$movedown."</small></td></tr>";
		$class = ($class == 'odd') ? 'even' : 'odd';
	}
	echo "<tr><td class='foot' colspan='5' align='center'>
	<input type='submit' name='submit' value='"._IM_NEW."'>
	</td></tr></table></form>";
	xoops_cp_footer();
}

function im_admin_clean() {
	global $xoopsDB;
	$i=0;
	$result = $xoopsDB->query("SELECT id FROM ".$xoopsDB->prefix("tmenu")." ORDER BY weight ASC");
	while (list($id) = $xoopsDB->fetchrow($result)) {
		$xoopsDB->queryF("UPDATE ".$xoopsDB->prefix("tmenu")." SET weight='$i' WHERE id=$id");
		$i++;
	}
}

 */

	xoops_cp_footer();

?>